package com.select.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.select.course.entity.CourseRoom;
import com.select.course.entity.SchoolCollege;
import com.select.course.mapper.CourseRoomMapper;
import com.select.course.service.AdminService;
import com.select.course.service.CourseRoomService;
import com.select.course.service.SchoolCollegeService;
import com.select.course.utils.PageUtils;
import com.select.course.utils.R;
import com.select.course.vo.CourseRoomVo;
import com.select.course.vo.PageVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 24820
 * @description 针对表【c_course_room】的数据库操作Service实现
 * @createDate 2022-10-28 14:00:26
 */
@Service
public class CourseRoomServiceImpl extends ServiceImpl<CourseRoomMapper, CourseRoom>
        implements CourseRoomService {

    @Resource
    @Lazy
    private AdminService adminService;
    @Resource
    private SchoolCollegeService schoolCollegeService;

    @Override
    public PageUtils getAllCourse(PageVo pageVo) {


        QueryWrapper<CourseRoom> wrapper = new QueryWrapper<>();
        Page<CourseRoom> courseRoomPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        String college = pageVo.getCollege();
        if (!StringUtils.isEmpty(college)) {
            SchoolCollege schoolCollege = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("name", college));
            wrapper.eq("college_id", schoolCollege.getId());
        }
        String query = pageVo.getQuery();
        if (!StringUtils.isEmpty(query)) {
            wrapper.and(e -> {
                e.like("id",query).or().like("room_name",query);
            });
        }
        Page<CourseRoom> page = this.page(courseRoomPage, wrapper);
        List<CourseRoom> records = page.getRecords();
        List<CourseRoomVo> collect = records.stream().map(e -> {
            CourseRoomVo courseRoomVo = new CourseRoomVo();
            courseRoomVo.setDescript(e.getDescript());
            courseRoomVo.setName(e.getRoomName());
            courseRoomVo.setRegisterTime(e.getRegistertime());
            courseRoomVo.setId(e.getId());
            return courseRoomVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return  pageUtils;
    }

    @Override
    public List<String> getClassRoom() {
        List<CourseRoom> list = this.list();
        List<String> collect = list.stream().map(e -> {
            String roomName = e.getRoomName();
            return roomName;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public R classRoomCheck(Long classroom) {
        CourseRoom courseRoom = this.getById(classroom);
        if (!StringUtils.isEmpty(courseRoom)) {
            CourseRoomVo courseRoomVo = new CourseRoomVo();
            courseRoomVo.setId(courseRoom.getId());
            courseRoomVo.setDescript(courseRoom.getDescript());
            courseRoomVo.setName(courseRoom.getRoomName());
            courseRoomVo.setRegisterTime(courseRoom.getRegistertime());
            return R.ok().put("data",courseRoomVo);
        }
        return R.error();
    }

    @Transactional
    @Override
    public R addClassRoom(CourseRoomVo courseRoomVo) {
        CourseRoom courseRoom1 = new CourseRoom();
        courseRoom1.setRoomName(courseRoomVo.getName());
        courseRoom1.setDescript(courseRoomVo.getDescript());
        courseRoom1.setRegistertime(new Date());
        this.save(courseRoom1);
        return R.ok();
    }

    @Override
    public R classRoomCheckByName(String classroom) {
        CourseRoom room_name = this.getOne(new QueryWrapper<CourseRoom>().eq("room_name", classroom));
        if (StringUtils.isEmpty(room_name)) {
            return R.ok();
        }
        return R.error(111, "该教室已存在！");
    }

    @Transactional
    @Override
    public R updateCourseRoom(CourseRoomVo courseRoomVo) {
        CourseRoom courseRoom = this.getOne(new QueryWrapper<CourseRoom>().eq("room_name", courseRoomVo.getName()));
        if (!StringUtils.isEmpty(courseRoom)) {
            CourseRoom courseRoom1 = new CourseRoom();
            courseRoom1.setRoomName(courseRoomVo.getName());
            courseRoom1.setRegistertime(new Date());
            courseRoom1.setDescript(courseRoomVo.getDescript());
            courseRoom1.setId(courseRoomVo.getId());
            this.updateById(courseRoom1);
            return R.ok();
        }

        return R.error(111, "该教室已存在！");


    }
}




